package com.yangqi.ThreadDemo;

/**
 * 如果我们希望当main线程结束后，子线程自动结束
 * 只需要将子线程设为守护线程即可
 * 设置守护线程 setDaemon
 */
public class Thread05 {
    public static void main(String[] args) {
        Cell cell = new Cell();
        new Thread(cell).start();
        new Thread(cell).start();
        new Thread(cell).start();
    }
}
class Cell implements Runnable{
    private static int ticketNum = 10;
    private boolean loop = true;
    public synchronized void sync(){
        if (ticketNum <= 0) {
            System.out.println("售票结束");
            loop = false;
            return;
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("窗口" + Thread.currentThread().getName() + "售出一张票" + "剩余票数" + (--ticketNum));
    }
    @Override
    public void run() {
        while(loop){
            sync();
        }
    }
}
